﻿using Direct.IDAL.Message;
using DirectSell.Common.Filter;
using DirectSell.Common.Page;
using DirectSell.Model.Message;
using DirectSell.Model.Query;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace DirectSell.WebLayer.Areas.Manager.Controllers
{
    /// <summary>
    /// 后台留言管理
    /// </summary>
    public class MessageManageController : Controller
    {
        #region 注入
        [Dependency]
        protected IMessageRepository _repository { get; set; }

        [Dependency]
        protected DirectSell.IMongoDAL.User.IMessageRepository _mongoRepository { get; set; }

        [Dependency]
        protected IMongoDAL.Manager.MessageManage.IMessageManageRepository _mrepository { get; set; }

        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [ManagerFilter(IsCheck =true)]
        public async Task<ActionResult> Index(BaseCondition condition)
        {
            condition.startTime = Request["startTime"];

            condition.endTime = Request["endTime"];

            if (!string.IsNullOrEmpty(condition.endTime))
            {
                condition.endTime = DateTime.Parse(condition.endTime).AddDays(1).ToString();
            }

            var list = await _mrepository.MyMessage(condition);

           // var pagelist = list.PageList(condition.pageIndex);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxSearchPost", list);

            return View(list);
        }
        #endregion

        #region 会员信息
        /// <summary>
        /// 会员信息
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public ActionResult MemberInfo(string usercode)
        {
            var info =  _repository.GetUserInfo(usercode);
            return PartialView(info);
        }
        #endregion

        #region 查询一条留言
        /// <summary>
        /// 查询一条留言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> ShowMsg(string id)
        {
            var info = await _mongoRepository.QueryMsg(id);
            return PartialView(info);
        }
        #endregion

        #region 标为已读
        /// <summary>
        /// 标为已读
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> AlRead(string id)
        {
            if ( !await _mongoRepository.AlRead(id))
            {
                return Json("失败");
            }
            return Json("成功");
        }
        #endregion

        #region 删除留言
        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> Delete(string id)
        {
            if ( ! await _mongoRepository.DeleteMessage(id))
            {
                return Json("失败");
            }
            return Json("删除成功");
        }
        #endregion

        #region 回复页面
        /// <summary>
        /// 回复页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Reply(string usercode)
        {
            ViewBag.usercode = usercode;
            return PartialView();
        }
        #endregion

        #region 回复留言
        /// <summary>
        /// 回复留言
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> SendMessage(MessageReq req)
        {
            req.SendName = "公司";
            req.SendCode = "admin";
            req.SendPhone = "暂未配置";
            if (!await _mongoRepository.InsertMessage(req))
            {
                return Json("留言失败");
            }
            return Json("留言成功");
        }
        #endregion

    }
}